package com.geeklan.smart.widget;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.geeklan.smart.R;
import com.geeklan.smart.databinding.PopupWindowLayoutMessageTypeBinding;
import com.geeklan.smart.utils.DensityUtils;

public class CustomPopupWindow implements View.OnClickListener {
    private PopupWindow popupWindow;
    private Activity activity;
    private int[] location = new int[2]; // 存储 titleView 的位置
    private PopupWindowLayoutMessageTypeBinding mBinding;
    private OnSelectedTypeListener onSelectedTypeListener;


    public CustomPopupWindow(Activity activity,OnSelectedTypeListener onSelectedTypeListener) {
        this.activity = activity;
        this.onSelectedTypeListener = onSelectedTypeListener;
        initialize();
    }

    private void initialize() {
        mBinding = PopupWindowLayoutMessageTypeBinding.inflate(activity.getLayoutInflater());

        // 2. 设置 PopupWindow
        popupWindow = new PopupWindow(mBinding.getRoot(), LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(activity, android.R.color.transparent)));
//        popupWindow.setFocusable(true);
//        popupWindow.setOutsideTouchable(true);

        // 设置透明度为0（完全透明）
        mBinding.gradientView.setAlpha(0f);

        // 5. 关闭 PopupWindow 时使用动画
        popupWindow.setOnDismissListener(() -> {
            mBinding.gradientView.setAlpha(0f); // 关闭时，恢复透明
            mBinding.getRoot().startAnimation(AnimationUtils.loadAnimation(activity, R.anim.popup_slide_out));
        });

        mBinding.llAllMessage.setOnClickListener(this);
        mBinding.llDeviceBugMessage.setOnClickListener(this);
        mBinding.llFeedPlanMessage.setOnClickListener(this);
        mBinding.llOfflineMessage.setOnClickListener(this);
        mBinding.llNullMessage.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        //0-全部消息 1-空仓提醒 2-设备离线 3-投喂提醒 4-设备故障
        if(id == mBinding.llAllMessage.getId()){
            onSelectedTypeListener.onSelected(0);
        } else if(id == mBinding.llNullMessage.getId()){
            onSelectedTypeListener.onSelected(1);
        } else if(id == mBinding.llOfflineMessage.getId()){
            onSelectedTypeListener.onSelected(2);
        } else if(id == mBinding.llDeviceBugMessage.getId()){
            onSelectedTypeListener.onSelected(4);
        } else if(id == mBinding.llFeedPlanMessage.getId()){
            onSelectedTypeListener.onSelected(3);
        }
        popupWindow.dismiss();
    }

    /**
     * 显示 PopupWindow，在指定的位置显示
     */
    public void showAtLocation(View titleView, int selectedIndex) {
        // 获取 titleView 在屏幕中的位置
        titleView.getLocationOnScreen(location);
        int titleBottom = location[1] + titleView.getHeight();

        // 6. 显示 PopupWindow
        popupWindow.showAtLocation(titleView, Gravity.TOP | Gravity.START, 0, titleBottom);
        //0-全部消息 1-空仓提醒 2-设备离线 3-投喂提醒 4-设备故障
        switch (selectedIndex) {
            case 0:
                mBinding.tvAllMessage.setTextColor(activity.getColor(R.color.color_9ccc9c));
                break;
            case 1:
                mBinding.tvNullMessage.setTextColor(activity.getColor(R.color.color_9ccc9c));
                break;
            case 2:
                mBinding.tvOfflineMessage.setTextColor(activity.getColor(R.color.color_9ccc9c));
                break;
            case 3:
                mBinding.tvFeedPlanMessage.setTextColor(activity.getColor(R.color.color_9ccc9c));
                break;
            case 4:
                mBinding.tvDeviceBugMessage.setTextColor(activity.getColor(R.color.color_9ccc9c));
                break;
//            case 5:
//                mBinding.tvInsectMessage.setTextColor(activity.getColor(R.color.color_9ccc9c));
//                break;
        }

        // 7. 显示动画
        mBinding.getRoot().startAnimation(AnimationUtils.loadAnimation(activity, R.anim.popup_slide_in));

        // 8. 使用 ValueAnimator 来实现渐变效果
        ValueAnimator fadeInAnimator = ValueAnimator.ofFloat(0f, 1f);
        fadeInAnimator.setDuration(1000); // 1秒动画
        fadeInAnimator.addUpdateListener(animation -> {
            // 更新渐变区域的透明度
            float alpha = (float) animation.getAnimatedValue();
            mBinding.gradientView.setAlpha(alpha); // 从透明变为黑色
        });

        // 启动渐变动画
        fadeInAnimator.start();
    }

    /**
     * 关闭 PopupWindow
     */
    public void dismiss() {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
    }

    /**
     * 检查 PopupWindow 是否正在显示
     */
    public boolean isShowing() {
        return popupWindow != null && popupWindow.isShowing();
    }


    public interface OnSelectedTypeListener{
        void onSelected(int type);
    }
}

